<template>
  <div>
    <div class="register">
      <h3>
        注册新用户
        <span class="go"
          >我有账号，去 <router-link to="/login">登陆</router-link>
        </span>
      </h3>
      <ValidationObserver v-slot="{ handleSubmit }">
        <form @submit.prevent="handleSubmit(onSubmit)">
          <ValidationProvider
            class="content"
            tag="div"
            mode="lazy"
            rules="phoneRequired|phoneNumber"
            v-slot="{ errors }"
          >
            <label>手机号:</label>
            <input
              type="text"
              placeholder="请输入你的手机号"
              v-model="user.phone"
            />
            <span ref="spanRef" class="error-msg">{{ errors[0] }}</span>
          </ValidationProvider>
          <ValidationProvider
            class="content"
            tag="div"
            mode="lazy"
            rules="codeRequired|codeNumber"
            v-slot="{ errors }"
          >
            <label>验证码:</label>
            <input type="text" placeholder="请输入验证码" v-model="user.code" />
            <input
              :style="phoneCode ? '' : 'opacity:0.5'"
              class="code"
              type="button"
              value="获取验证码"
              @click="codeHandle"
            />
            <span class="error-msg">{{ errors[0] }}</span>
          </ValidationProvider>
          <ValidationProvider
            class="content"
            tag="div"
            mode="lazy"
            rules="passwordRequired|passwordNumber"
            v-slot="{ errors }"
          >
            <label>登录密码:</label>
            <input
              type="text"
              placeholder="请输入你的登录密码"
              v-model="user.password"
            />
            <span class="error-msg">{{ errors[0] }}</span>
          </ValidationProvider>
          <ValidationProvider
            class="content"
            tag="div"
            mode="lazy"
            :rules="`passwordRequired|passwordNumber|againpasswordNumber:${user.password}`"
            v-slot="{ errors }"
          >
            <label>确认密码:</label>
            <input
              type="text"
              placeholder="请输入确认密码"
              v-model="user.againpassword"
            />
            <span class="error-msg">{{ errors[0] }}</span>
          </ValidationProvider>
          <ValidationProvider
            tag="div"
            v-slot="{ errors }"
            class="controls"
            rules="checked"
          >
            <label>
              <input name="m1" type="checkbox" v-model="user.isChecked" />
              <span>同意协议并注册《尚品汇用户协议》</span>
            </label>
            <span class="error-msg">{{ errors[0] }}</span>
          </ValidationProvider>
          <div class="btn">
            <button :style="bool ? '' : 'opacity:0.5'" :disabled="!bool">
              完成注册
            </button>
          </div>
        </form>
      </ValidationObserver>
    </div>
    <LoginRegisterFooter />
  </div>
</template>

<script>
import { ValidationObserver, ValidationProvider, extend } from "vee-validate";
import { required } from "vee-validate/dist/rules";
import { phoneReg, codeReg, passwordReg } from "@/utils/reg";
import { getcode, registerUser } from "@/api";

extend("phoneRequired", {
  ...required,
  message: "手机号不能为空！",
});
extend("codeRequired", {
  ...required,
  message: "请输入验证码",
});
extend("passwordRequired", {
  ...required,
  message: "密码不能为空！",
});
extend("phoneNumber", {
  validate(value) {
    return phoneReg.test(value);
  },
  message: "手机号格式错误,请重新输入",
});
extend("codeNumber", {
  validate(value) {
    return codeReg.test(value);
  },
  message: "验证码格式错误,请重新输入",
});
extend("passwordNumber", {
  validate(value) {
    return passwordReg.test(value);
  },
  message: "密码格式错误,请重新输入(6至12位的大小写字母、字符或数字)",
});
extend("againpasswordNumber", {
  validate(value, { password }) {
    return password === value;
  },
  params: ["password"],
  message: "与登录密码不一致,请重新输入",
});
extend("checked", {
  validate(value) {
    return value;
  },
  message: "请阅读详情并同意",
});

export default {
  name: "Register",
  data() {
    return {
      user: {
        isChecked: false,
        phone: "",
        code: "",
        password: "",
        againpassword: "",
      },
    };
  },
  computed: {
    bool() {
      let { phone, code, password, againpassword, isChecked } = this.user;
      return phone && code && password && againpassword && isChecked;
    },
    phoneCode() {
      const phone = this.user.phone.trim();
      return phoneReg.test(phone);
    },
  },
  methods: {
    async onSubmit() {
      const { phone, code, password } = this.user;
      const result = await registerUser({ phone, code, password });
      if (result.code !== 200) return alert(result.message);
      alert("恭喜你注册成功");
      this.$router.push("/login");
    },
    async codeHandle() {
      const phone = this.user.phone.trim();
      if (!phone) return;
      const result = await getcode(phone);
      if (result.code === 200) {
        console.log(result.data);
      } else {
        console.log(result.message);
      }
    },
  },
  components: {
    ValidationObserver,
    ValidationProvider,
  },
};
</script>

<style lang='less' scoped>
.register {
  width: 1200px;
  height: 445px;
  border: 1px solid rgb(223, 223, 223);
  margin: 0 auto;

  h3 {
    background: #ececec;
    margin: 0;
    padding: 6px 15px;
    color: #333;
    border-bottom: 1px solid #dfdfdf;
    font-size: 20.04px;
    line-height: 30.06px;

    span {
      font-size: 14px;
      float: right;

      a {
        color: #e1251b;
      }
    }
  }

  div:nth-of-type(1) {
    margin-top: 40px;
  }

  .content {
    padding-left: 390px;
    margin-bottom: 18px;
    position: relative;

    .code {
      width: 90px;
      background-color: red;
      color: #fff;
    }

    label {
      font-size: 14px;
      width: 96px;
      text-align: right;
      display: inline-block;
    }

    input {
      width: 270px;
      height: 38px;
      padding-left: 8px;
      box-sizing: border-box;
      margin-left: 5px;
      outline: none;
      border: 1px solid #999;
    }

    img {
      vertical-align: sub;
    }

    .error-msg {
      position: absolute;
      top: 100%;
      left: 495px;
      color: red;
    }
  }

  .controls {
    text-align: center;
    position: relative;

    input {
      vertical-align: middle;
    }

    .error-msg {
      position: absolute;
      top: 100%;
      left: 495px;
      color: red;
    }
  }

  .btn {
    text-align: center;
    line-height: 36px;
    margin: 17px 0 0 55px;

    button {
      outline: none;
      width: 270px;
      height: 36px;
      background: #e1251b;
      color: #fff !important;
      display: inline-block;
      font-size: 16px;
    }
  }
}
</style>